define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {
    //读取选中的条目
    $.jstree.core.prototype.get_all_checked = function (full) {
        var obj = this.get_selected(), i, j;
        for (i = 0, j = obj.length; i < j; i++) {
            obj = obj.concat(this.get_node(obj[i]).parents);
        }
        obj = $.grep(obj, function (v, i, a) {
            return v != '#';
        });
        obj = obj.filter(function (itm, i, a) {
            return i == a.indexOf(itm);
        });
        return full ? $.map(obj, $.proxy(function (i) {
            return this.get_node(i);
        }, this)) : obj;
    };
    var Controller = {
        index: function () {
        	var $url=$("#ajax_url").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: $url,
                    //edit_url: 'recruit/apply/edit',
                    //del_url: 'recruit/apply/del',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'apply_id',
                sortName: 'apply_id',
                columns: [
                    [
                        {field: 'apply_id', title: '编号'},
                        {field: 'apply_name', title: '姓名'},
                        {field: 'recruit_title', title: '招聘标题'},
                        {field: 'recruit_post_name', title: '报考岗位'},
                        {field: 'createtime', title: '报名时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '更新时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: '状态', formatter: Table.api.formatter.status},
                        {field: 'operate', title: '操作',formatter: function (value, row, index) {
                        	if(row.status=='wait'||row.status=='reject'){
                        		return '<a href="../user/apply/recruit_id/'+row.recruit_id+'/apply_id/'+row.apply_id+'" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i>修改</a>';
                        	}
                        	if(row.status=='pay'){
                        		return '<a href="../user/pay/recruit_id/'+row.recruit_id+'/apply_id/'+row.apply_id+'" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i>去付款</a>';
                        	}
                        	if(row.status=='exam_downloading'||row.status=='exam_downloaded'){
                        		return '<a href="../user/exam_down/recruit_id/'+row.recruit_id+'/apply_id/'+row.apply_id+'" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i>下载</a>';
                        	}
                        	if(row.status=='interview_downloading'||row.status=='interview_downloaded'){
                        		return '<a href="../user/interview_down/recruit_id/'+row.recruit_id+'/apply_id/'+row.apply_id+'" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i>下载</a>';
                        	}
                            
                        }
                       }
                    ]
                ],
                showExport: false,
                showToggle: false,
                showColumns: false,
	            pagination: false,
	            search: false,
	            commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").size() > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[rules]']").val(r.join(','));
                    }
                    return true;
                });
                //渲染权限节点树
                //销毁已有的节点树
                $("#treeview").jstree("destroy");
                Controller.api.rendertree(nodeData);
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
                $("select[name='row[pid]']").trigger("change");
            },
            rendertree: function (content) {
                $("#treeview")
                        .on('redraw.jstree', function (e) {
                            $(".layer-footer").attr("domrefresh", Math.random());
                        })
                        .jstree({
                            "themes": {"stripes": true},
                            "checkbox": {
                                "keep_selected_style": false,
                            },
                            "types": {
                                "root": {
                                    "icon": "fa fa-folder-open",
                                },
                                "menu": {
                                    "icon": "fa fa-folder-open",
                                },
                                "file": {
                                    "icon": "fa fa-file-o",
                                }
                            },
                            "plugins": ["checkbox", "types"],
                            "core": {
                                'check_callback': true,
                                "data": content
                            }
                        });
            }
        }
    };
    return Controller;
});